<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>补货审核</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <base href="<%=url%>">
    <script src="https://lib.baomitu.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://lib.baomitu.com/layui/2.6.5/layui.min.js"></script>
    <link href="https://lib.baomitu.com/layui/2.6.5/css/layui.min.css" rel="stylesheet">
    <style>
    </style>
</head>
<body>
<div class="layui-main">
<blockquote class="layui-elem-quote">列表</blockquote>
<a style="margin-left: 50px"><span style="font-size: 18px;margin-right: 10px">订单编号查询:</span><input type="text" placeholder="请输入订单编号" id="input-id" style="height: 30px"></a>
<a style="margin-left: 50px"><span style="font-size: 18px;margin-right: 10px">药品名称查询:</span><input type="text" placeholder="请输入药品名称" id="input-ypname" style="height: 30px"></a>
<button  style="height: 30px;background-color: #1e9fff;color: whitesmoke;width: 60px;border:1px #1e9fff; border-radius: 4px" id="select_id_ypName">搜索</button>
<!--信息存放位置 -->
<table id="tbl" lay-filter="tbl"></table>
<script type="text/html" id="tbl-toolbar">

     {{#  if(d.staticc == 0){ }}
           <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="edit-Check">审核</a>
     {{#  }else{ }}
            <a class="layui-btn layui-btn-sm" >审核通过</a>
     {{#  } }}
</script>
    <!-- 补货弹出层 -->
    <div id="insert-layer" style="  display: none; padding: 20px">
        <form id="insert-form" class="layui-form layui-form-pane" lay-filter="insert-form">
            <div class="layui-container" style="width: 100%;">
                <div class="layui-row">
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">订单编号</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="id" name="id" class="layui-input" lay-verify="required">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品编号</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="ypid" name="ypid" class="layui-input" lay-verify="required">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品名称</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" name="ypname" class="layui-input" lay-verify="required">
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品规格</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" name="normsid" class="layui-input" lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品单位</label>
                            <div class="layui-input-block">
                                <label>
                                    <select name="unitid" class="unitid"></select>
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品价格</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="price" name="price" class="layui-input" lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药品进价</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="jprice" name="jprice" class="layui-input" lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">进货数量</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="jnum" name="jnum" class="layui-input" lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">药房类型</label>
                            <div class="layui-input-block">
                                <label>
                                    <select name="yfid" class="yfid"></select>
                                </label>
                            </div>
                        </div>
                     </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">申请人</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" name="sname" class="layui-input" disabled lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">申请时间</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="datetime" name="stime" class="layui-input" lay-verify="required" autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">审核人</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" id="shname" name="Shname" class="layui-input"  disabled autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-xs6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <label>
                                    <input type="text" name="notes" class="layui-input"  autocomplete="off">
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-sm10">
                        <div class="layui-form-item" style="text-align: center">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="insert-form-submit">确定</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/html" id="tooll">
<!--  //  <! &#45;&#45;添加审核状态按钮 &ndash;&gt;-->
    {{#  if(d.staticc == 1){ }} <!--d相当于layui里边的field -->
    <button style="color: blue">已审核</button>
    {{#  } else { }}
    <button id="audit" style="color: red">未审核</button>
    {{#  } }}

</script>


</script>
<script>
    //设置时间格式
    function myDateFomat(t){
        let date=new Date(t);
        let y=date.getFullYear();
        let m=(date.getMonth()+1).toString().padStart(2,"0");
        let d=date.getDate().toString().padStart(2,"0");
        let h=date.getHours().toString().padStart(2,"0");
        let f=date.getMinutes().toString().padStart(2,"0");
        let s=date.getSeconds().toString().padStart(2,"0");
        return y+"-"+m+"-"+d+" "+h+":"+f+":"+s;
    }
    //遍历下拉框内容
    $(function () {
        $.get("/medicine/SelectUnit",function (date,res) {
            if(res=="success"){
                $(".unitid").empty();
                for (let i = 0; i < date.length; i++) {
                    $(".unitid").append("<option value="+date[i].unitid+">"+date[i].unitname+"</option>")

                }
            }
        });
        setTimeout(function () {
            $.get("/medicine/SelectHome",function (date,res) {
                if(res=="success"){
                    $(".yfid").empty();
                    for (let i = 0; i < date.length; i++) {
                        $(".yfid").append("<option value="+date[i].yfid+">"+date[i].yfname+"</option>")

                    }
                }
            });
        },500)

    })
    layui.use(['jquery', 'table', 'layer', 'form'], function () {
        var $ = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        table.render({
            elem: '#tbl',
            url: '/check/selectAll',
            method: 'get',
            initSort: { //排序
                field: 'account' //排序字段，对应 cols 设定的各字段名
                , type: 'asc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            cols: [[
                {field: 'id', title: '编号', width: 60},
                {field: 'ypid', title: '药品编号', width: 90},
                {field: 'ypname', title: '药品名称', width: 150},
                {field: 'normsid', title: '药品规格', width: 100},
                {field: 'unitid', title: '药品单位', templet:function (d) {
                        return d.unit && d.unit.unitname
                    },width: 88},
                {field: 'jprice', title: '药品进价', width: 88},
                {field: 'price', title: '药品售价', width: 88},
                {field: 'jnum', title: '进货数量', width: 88},
                {field: 'yfid', title: '药房编号', templet:function (d) {
                        return d.dispensary && d.dispensary.yfname
                    },width: 98},
                {field: 'sname', title: '申请人', width: 90},
                {field: 'staticc', title: '状态', templet:'#tooll',width: 90},
                {field: 'stime', title: '申请时间', width: 127,
                    templet: "<div>{{layui.util.toDateString(d.stime, 'yyyy-MM-dd HH:mm:ss')}}</div>", width: 130},
                {field: 'notes', title: '备注', width: 127},
                {toolbar: '#tbl-toolbar', title: '操作', width: 90}
            ]],
            page: {
                limit: 5,//指定每页显示的条数
                limits: [5, 10, 20, 40, 50, 60, 80, 100]//每页条数的选中
            }//开启分页

            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            },
            // 表格容器id，用于表格重载
            id: 'tbl'
        });
        //精确查询  根据jid查询
        $("#select_id_ypName").click(function () {
            var id=$("#input-id").val();
            var ypname=$("#input-ypname").val();
            table.render({
                elem: '#tbl',
                url: '/check/select_id_ypName?id='+id+'&ypname='+ypname,
                method: 'get',
                cols: [[
                    {field: 'id', title: '编号', width: 60},
                    {field: 'ypid', title: '药品编号', width: 90},
                    {field: 'ypname', title: '药品名称', width: 150},
                    {field: 'normsid', title: '药品规格', width: 100},
                    {field: 'unitid', title: '药品单位', templet:function (d) {
                            return d.unit && d.unit.unitname
                        },width: 88},
                    {field: 'jprice', title: '药品进价', width: 88},
                    {field: 'price', title: '药品售价', width: 88},
                    {field: 'jnum', title: '进货数量', width: 88},
                    {field: 'yfid', title: '药房编号', templet:function (d) {
                            return d.dispensary && d.dispensary.yfname
                        },width: 98},
                    {field: 'sname', title: '申请人', width: 90},
                    {field: 'staticc', title: '状态', templet:'#tooll',width: 90},
                    {field: 'stime', title: '申请时间', width: 127,
                        templet: "<div>{{layui.util.toDateString(d.stime, 'yyyy-MM-dd HH:mm:ss')}}</div>", width: 130},
                    {field: 'notes', title: '备注', width: 127},
                    {toolbar: '#tbl-toolbar', title: '操作', width: 90}
                ]],

                page:{
                    limit:5,//指定每页显示的条数
                    limits:[5,10,20,40,50,60,80,100]//每页条数的选中
                }//开启分页

                , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                    var result;
                    if (this.page.curr) {
                        result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                    }
                    else {
                        result = res.data.slice(0, this.limit);
                    }
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": result //解析数据列表
                    };
                },
                // 表格容器id，用于表格重载
                id: 'tbl'
            });
        })

        table.on('tool(tbl)', function (obj) {
            // 获取当前行数据和lay-event的值
            var data = obj.data;
            var event = obj.event;
            // 补货数据回显
            if (event === 'edit-Check') {
                // 表格数据回显
                form.val('insert-form', {
                    "id":data.id,
                    "ypid": data.ypid,
                    "ypname": data.ypname,
                    "normsid": data.normsid,
                    "unitid": data.unit.unitid,
                    "jprice":data.jprice,
                    "price": data.price,
                    "jnum": data.jnum,
                    "yfid": data.dispensary.yfid,
                    "sname":data.sname,
                    "stime":myDateFomat(data.stime),
                    "notes": data.notes
                });
                // 显示补货表单的弹出层
                UpIndex = layer.open({
                    type: 1,
                    title: '补货信息',
                    skin: 'layui-layer-molv',
                    area: ['800px'],
                    content: $('#insert-layer')
                });
                // 表单提交
                form.on('submit(insert-form-submit)',function (data) {
                    var price = $("#price").val(); // 售价
                    var jprice = $("#jprice").val(); // 进价

                    if(Number(price) <= Number(jprice)){
                        layer.msg("进价只能小于售价");
                    } else {
                        setTimeout(function () {
                            updatekc()
                            table.reload('tbl');
                        }, 100)
                        setTimeout(function () {
                            updateStatus()
                            table.reload('tbl');
                        }, 200)

                        // ajax方式修改
                        $.ajax({
                            url: "/receipts/insert",
                            type: "get",
                            data: data.field,
                            contentType: 'application/json',
                            dataType: 'json',
                            success: function (data) {
                                if (data.status === 1) {
                                    layer.close(UpIndex);
                                    layer.msg('提交成功');
                                    table.reload('tbl');
                                } else {
                                    layer.msg('提交失败');
                                }
                            },
                            error: function () {
                                console.log("ajax error");
                            }
                        });
                    }
                    // 阻止表单跳转
                    return false;
                });

            }

        });


    })

</script>
<script>
    //封装修改图书库存方法
    function updatekc(){
        let ypid= $("#ypid").val();
        let jnum= $("#jnum").val();
        $.ajax({
            url: "/medicine/updateCK?ypid="+ypid+"&jnum="+jnum,
            type: "get",
            contentType: 'application/json',
            dataType: 'json',
            success: function (data) {
                if (data.status === 1) {
                    layer.close(layer.index);
                } else {
                }
            },
            error: function () {
                console.log("ajax error");
            }
        });
    }
    //封装修改审核状态方法
    function  updateStatus(){
        let id= $("#id").val();
        $.ajax({
            url: "/check/updateZT?id="+id,
            type: "get",
            contentType:'application/json',
            dataType: 'json',
            success: function (data) {
                if (data.status === 1) {
                    layer.close(layer.index);
                } else {
                }
            },
            error: function () {
                console.log("ajax error");
            }
        });
    }

</script>



<script>
    $(function () {
        setTimeout(function () {
            $.get("/loginName",function (date) {
                $("#shname").val(date)
            })
        },100)

    })
</script>


</body>
</html>